<?php
/**
 * 网址转换
 * 
 * $Id: url.php 121 2010-05-28 07:57:29Z Administrator $
 * $Revision: 121 $
 * $Author: Administrator $
 * $Date: 2010-05-28 07:57:29 +0000 (Fri, 28 May 2010) $
 * @author glzaboy<glzaboy@163.com>
 * @package fastlib
 * @subpackage classes
 * @copyright 2005-2010 (c) glzaboy
 */
if (! defined ( '__FASTLIB__' )) {
	if (__DEBUG__) {
		die ( sprintf ( 'Forbidden 403 at file %s on line %d', __FILE__, __LINE__ ) );
	} else {
		die ( 'Forbidden 403' );
	}
}
/**
 * 网址转换
 *
 */
class url {
	/**
	 * 地址编码
	 * 
	 * 将常规地址串编码成下载工具可以识别的地址串。
	 * 
	 * 编码方法主要有url,qqdl,flashget,thunder
	 * 分别对应urlencode ，QQ旋风，网际快车，讯雷
	 *
	 * @param string $url 要编码的地址
	 * @param string $option 编码的方法
	 * @return string 返回编码后的字符
	 */
	public function encode($url, $option = "url") {
		switch (strtolower ( $option )) {
			case 'url' :
				return urlencode ( $url );
				break;
			case 'qqdl' :
				return 'qqdl://' . base64_encode ( $url );
				break;
			case 'flashget' :
				return 'flashget://' . base64_encode ( '[FLASHGET]' . $url . '[FLASHGET]' );
				break;
			case 'thunder' :
				return 'thunder://' . base64_encode ( 'AA' . $url . 'ZZ' );
				break;
		}
		return '';
	}
	/**
	 * 解码地址字符串
	 * 
	 * 可以解开QQ旋风，网际快车，讯雷等下载工具的地址
	 *
	 * @param string $url 要解码的地址串
	 * @return string 解码后的地址串
	 */
	public function decode($url) {
		if (strpos ( $url, "qqdl://" ) === 0) {
			return base64_decode ( substr ( $url, 7 ) );
		}
		if (strpos ( $url, "flashget://" ) === 0) {
			return substr ( base64_decode ( substr ( $url, 11 ) ), 10, - 10 );
		}
		if (strpos ( $url, "thunder://" ) === 0) {
			return substr ( base64_decode ( substr ( $url, 10 ) ), 2, - 2 );
		}
		return urldecode ( $url );
	}
	/**
	 * 返回本模块信息
	 *
	 * @return string
	 */
	public function printInfo() {
		$version = '$Revision: 121 $';
		$moudle = __CLASS__;
		$return = '';
		$return .= startTable ();
		$return .= printRow ( array (array ('class' => 'h', 'content' => '<center>' . $moudle . '</center>', 'colspan' => '2' ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'author' ), array ('class' => 'v', 'content' => 'glzaboy' ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'version' ), array ('class' => 'v', 'content' => $version ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'thunder support' ), array ('class' => 'v', 'content' => 'yes' ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'flashget support' ), array ('class' => 'v', 'content' => 'yes' ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'qqdl support' ), array ('class' => 'v', 'content' => 'yes' ) ) );
		$return .= endTable ();
		return $return;
	}
}